﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using TCPIPControl.StructHelper;
using System.Windows.Forms.DataVisualization.Charting;

namespace VibrationTest.OnLineChart
{
    public partial class frmLineChart : Form
    {
        public frmLineChart()
        {
            InitializeComponent();
        }

        private void frmLineChart_Load(object sender, EventArgs e)
        {
            this.chart1.Series.Clear();
            this.timer1.Start();
        }

        public Common.DBOper.Cls_Channel clschannel = null;

        public string ChartTypeName = "";
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            try
            {


                List<float> displayvalue = new List<float>();
                if (Mdl_Class.onlinchannelData.Count >0)
                {
                    object accdata = Mdl_Class.onlinchannelData.ToArray()[0];
                    int datachannelid = 0;
                    Mdl_Class.WriteLog.sysErrLog("刷新图表开始", ChartTypeName + "  通道" + Mdl_Class.OnLineChannelId.ToString());
                    switch (Mdl_Class.LineNO)
                    {

                        case 256:
                            Acceleration_256 accdate_256 = (Acceleration_256)accdata;
                            datachannelid = accdate_256.ChannelId;
                            switch (ChartTypeName)
                            {
                                case "频谱数据":
                                    displayvalue = accdate_256.accelerationFFTAmpDB.ToList();
                                    break;
                                case "时域数据":
                                    displayvalue = accdate_256.accelerationTimewave.ToList();
                                    break;
                                default:
                                    break;
                            }
                            this.lb_highdB.Text = accdate_256.highdB.ToString();
                            this.lb_lowdB.Text = accdate_256.lowdB.ToString();
                            this.lb_totaldB.Text = accdate_256.totaldB.ToString();
                            this.lb_velocityRMS.Text = accdate_256.velocityRMS.ToString();
                            break;
                        case 512:
                            Acceleration_512 accdate_512 = (Acceleration_512)accdata;
                            datachannelid = accdate_512.ChannelId;
                            switch (ChartTypeName)
                            {
                                case "频谱数据":
                                    displayvalue = accdate_512.accelerationFFTAmpDB.ToList();
                                    break;
                                case "时域数据":
                                    displayvalue = accdate_512.accelerationTimewave.ToList();
                                    break;
                                default:
                                    break;
                            }
                            this.lb_highdB.Text = accdate_512.highdB.ToString();
                            this.lb_lowdB.Text = accdate_512.lowdB.ToString();
                            this.lb_totaldB.Text = accdate_512.totaldB.ToString();
                            this.lb_velocityRMS.Text = accdate_512.velocityRMS.ToString();
                            break;
                        case 1024:
                            Acceleration_1024 accdate_1024 = (Acceleration_1024)accdata;
                            datachannelid = accdate_1024.ChannelId;
                            switch (ChartTypeName)
                            {
                                case "频谱数据":
                                    displayvalue = accdate_1024.accelerationFFTAmpDB.ToList();
                                    break;
                                case "时域数据":
                                    displayvalue = accdate_1024.accelerationTimewave.ToList();
                                    break;
                                default:
                                    break;
                            }
                            this.lb_highdB.Text = accdate_1024.highdB.ToString();
                            this.lb_lowdB.Text = accdate_1024.lowdB.ToString();
                            this.lb_totaldB.Text = accdate_1024.totaldB.ToString();
                            this.lb_velocityRMS.Text = accdate_1024.velocityRMS.ToString();
                            break;
                        case 2048:
                            Acceleration_2048 accdate_2048 = (Acceleration_2048)accdata;
                            datachannelid = accdate_2048.ChannelId;
                            switch (ChartTypeName)
                            {
                                case "频谱数据":
                                    displayvalue = accdate_2048.accelerationFFTAmpDB.ToList();
                                    break;
                                case "时域数据":
                                    displayvalue = accdate_2048.accelerationTimewave.ToList();
                                    break;
                                default:
                                    break;
                            }
                            this.lb_highdB.Text = accdate_2048.highdB.ToString();
                            this.lb_lowdB.Text = accdate_2048.lowdB.ToString();
                            this.lb_totaldB.Text = accdate_2048.totaldB.ToString();
                            this.lb_velocityRMS.Text = accdate_2048.velocityRMS.ToString();
                            break;
                        case 4096:
                            Acceleration_4096 accdate_4096 = (Acceleration_4096)accdata;
                            datachannelid = accdate_4096.ChannelId;
                            switch (ChartTypeName)
                            {
                                case "频谱数据":
                                    displayvalue = accdate_4096.accelerationFFTAmpDB.ToList();
                                    break;
                                case "时域数据":
                                    displayvalue = accdate_4096.accelerationTimewave.ToList();
                                    break;
                                default:
                                    break;
                            }
                            this.lb_highdB.Text = accdate_4096.highdB.ToString();
                            this.lb_lowdB.Text = accdate_4096.lowdB.ToString();
                            this.lb_totaldB.Text = accdate_4096.totaldB.ToString();
                            this.lb_velocityRMS.Text = accdate_4096.velocityRMS.ToString();
                            break;
                        case 8192:
                            Acceleration_8192 accdate_8192 = (Acceleration_8192)accdata;
                            datachannelid = accdate_8192.ChannelId;
                            switch (ChartTypeName)
                            {
                                case "频谱数据":
                                    displayvalue = accdate_8192.accelerationFFTAmpDB.ToList();
                                    break;
                                case "时域数据":
                                    displayvalue = accdate_8192.accelerationTimewave.ToList();
                                    break;
                                default:
                                    break;
                            }
                            this.lb_highdB.Text = accdate_8192.highdB.ToString();
                            this.lb_lowdB.Text = accdate_8192.lowdB.ToString();
                            this.lb_totaldB.Text = accdate_8192.totaldB.ToString();
                            this.lb_velocityRMS.Text = accdate_8192.velocityRMS.ToString();
                            break;
                        case 16384:
                            Acceleration_16384 accdate_16384 = (Acceleration_16384)accdata;
                            datachannelid = accdate_16384.ChannelId;
                            switch (ChartTypeName)
                            {
                                case "频谱数据":
                                    displayvalue = accdate_16384.accelerationFFTAmpDB.ToList();
                                    break;
                                case "时域数据":
                                    displayvalue = accdate_16384.accelerationTimewave.ToList();
                                    break;
                                default:
                                    break;
                            }
                            this.lb_highdB.Text = accdate_16384.highdB.ToString();
                            this.lb_lowdB.Text = accdate_16384.lowdB.ToString();
                            this.lb_totaldB.Text = accdate_16384.totaldB.ToString();
                            this.lb_velocityRMS.Text = accdate_16384.velocityRMS.ToString();
                            break;
                        case 32768:
                            Acceleration_32768 accdate_32768 = (Acceleration_32768)accdata;
                            datachannelid = accdate_32768.ChannelId;
                            switch (ChartTypeName)
                            {
                                case "频谱数据":
                                    displayvalue = accdate_32768.accelerationFFTAmpDB.ToList();
                                    break;
                                case "时域数据":
                                    displayvalue = accdate_32768.accelerationTimewave.ToList();
                                    break;
                                default:
                                    break;
                            }
                            this.lb_highdB.Text = accdate_32768.highdB.ToString();
                            this.lb_lowdB.Text = accdate_32768.lowdB.ToString();
                            this.lb_totaldB.Text = accdate_32768.totaldB.ToString();
                            this.lb_velocityRMS.Text = accdate_32768.velocityRMS.ToString();
                            break;
                        case 65536:
                            Acceleration_65536 accdate_65536 = (Acceleration_65536)accdata;
                            datachannelid = accdate_65536.ChannelId;
                            switch (ChartTypeName)
                            {
                                case "频谱数据":
                                    displayvalue = accdate_65536.accelerationFFTAmpDB.ToList();
                                    break;
                                case "时域数据":
                                    displayvalue = accdate_65536.accelerationTimewave.ToList();
                                    break;
                                default:
                                    break;
                            }
                            this.lb_highdB.Text = accdate_65536.highdB.ToString();
                            this.lb_lowdB.Text = accdate_65536.lowdB.ToString();
                            this.lb_totaldB.Text = accdate_65536.totaldB.ToString();
                            this.lb_velocityRMS.Text = accdate_65536.velocityRMS.ToString();
                            break;
                        default:
                            break;
                    }
                    Mdl_Class.onlinchannelData.Clear();
                    double max = -99999999.0;
                    double min = 999999999999999;
                    if (datachannelid == Mdl_Class.OnLineChannelId)
                    {

                        if (displayvalue != null && displayvalue.Count > 0)
                        {

                            double[] x_value = new double[displayvalue.Count];
                            double[] y_value = new double[displayvalue.Count];
                            for (int i = 0; i < displayvalue.Count; i++)
                            {

                                if (ChartTypeName == "频谱数据")
                                {
                                    //x_value[i] = i + 1;
                                    x_value[i]= Math.Round((double)(i + 1) * Math.Round(Convert.ToDouble(Mdl_Class.SampFreq) / Convert.ToDouble(Mdl_Class.LineNO), 2),2);
                                }
                                else 
                                {
                                    x_value[i]= (double)(i + 1) / (Convert.ToDouble(Mdl_Class.SampFreq) * 2.0);
                                }
                                y_value[i] = Math.Round(Convert.ToDouble(displayvalue[i]), 3);
                                if (y_value[i]> max)
                                {
                                    max = y_value[i];
                                }
                                if (y_value[i]< min) 
                                {
                                    min = y_value[i];
                                }
                            }
                            this.chart1.Series.Clear();
                           
                            if (ChartTypeName == "频谱数据")
                            {
                                this.chart1.ChartAreas[0].AxisX.Maximum = Mdl_Class.SampFreq;
                                this.chart1.ChartAreas[0].AxisX.IsLogarithmic = true;
                            }
                            else 
                            {
                                this.chart1.ChartAreas[0].AxisX.Maximum = displayvalue.Count;
                            }
                            if (ChartTypeName== "时域数据")
                            {
                                this.chart1.ChartAreas[0].AxisY.Maximum = max*2 ;
                                this.chart1.ChartAreas[0].AxisY.Minimum = min*2;
                            }
                            //addCustLabels_xx();
                            Series ser = new Series();
                            ser.ChartType = SeriesChartType.Line;
                            ser.Color = Color.Red;
                            ser.Points.DataBindXY(x_value, y_value);
                            ser.Name = clschannel.TestPointName;
                            ser.LegendText = clschannel.TestPointName;
                            this.chart1.Series.Add(ser);

                            if (ChartTypeName.Contains("原始数据") || ChartTypeName.Contains("时域"))
                            {
                                this.chart1.ChartAreas[0].AxisX.Title = "单位(s)";
                                this.chart1.ChartAreas[0].AxisY.Title = "";
                            }
                            else
                            {
                                this.chart1.ChartAreas[0].AxisX.Title = "单位(HZ)";
                                this.chart1.ChartAreas[0].AxisY.Title = "单位(dB)";
                            }
                            Mdl_Class.WriteLog.sysErrLog("刷新图表完成", ChartTypeName+"  通道"+ datachannelid.ToString());
                        }
                    }
                }

            }
            catch (Exception ex)
            {

                Mdl_Class.WriteLog.sysErrLog("刷新图表",ex.Message.ToString());
            }

            timer1.Start();
        }

        private void chart1_MouseClick(object sender, MouseEventArgs e)
        {

        }
    }
}
